
<template>
  <MainSlot v-loading="loading">
    <template v-slot:searchForm>
      <SearchForm v-if="formItemList.length" :formItemList="formItemList" :unfoldFlag="unfoldFlag" @search="search"
        @isUnfold="(v) => { unfoldFlag = v }">
      </SearchForm>
    </template>
    <template v-slot:btn>
      <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
    </template>
    <template v-slot:table>
      <TableData :config="table_config" :tableThead="tableThead" :dataList="dataList" :loading="loading"
        @selection-change="selectionChange">
        <template v-slot:fileIdList="{ row }">
          <el-image v-if="row.imgList && row.imgList.length > 0" class="imageStyle" :src="row.imgList[0]"
            :preview-src-list="row.imgList">
          </el-image>
          <span v-else>无特征图片</span>
        </template>
      </TableData>
    </template>
    <template v-slot:pagination>
      <Pagination @size-Change="handleSizeChange" :size="size"  @current-change="handleCurrentChange" :page="page" :total="total">
      </Pagination>
    </template>
    <template v-slot:dialogModule>
      <AddEdit :title="'症状'" :diagnosisItemList="diagnosisItemList" @closeDialog="closeDialog" :addEditFlag="addEditFlag"
        :dialogVisible="dialogVisible" :data="formData">
      </AddEdit>
    </template>
  </MainSlot>
</template>
<script>
import MainSlot from "@/components/MainSlot";
import Pagination from '@/components/Pagination';
import SearchForm from '@/components/SearchForm';
import TableData from '@/components/TableData';
import BtnSlot from '@/components/BtnSlot';
import AddEdit from './components/addEdit.vue';
import diagnosticItemManagement from '@/api/diseaseBankManagement/diagnosticItemManagement.js';
import diagnosticItemSymptom from '@/api/diseaseBankManagement/diagnosticItemSymptom.js';

import varietyManagement from '@/api/basicInformation/varietyManagement.js';

export default {
  components: { MainSlot, Pagination, SearchForm, TableData, BtnSlot, AddEdit },
  data() {
    return {
      unfoldFlag: false,
      loading: false,
      dialogVisible: false,
      latLngList: [],
      searchFormData:{},
      btnList: ['add', 'delete'],
      addEditFlag: 'add',
      formData: {},
      page: 1,
      size:10,
      total:100,
      selectIndex: 0,
      dataList: [
        {
          item1: 'F实验室监测',
          item2: 'F4',
          item3: '病原鉴定报告检出荧光假单胞菌',
          item4: ''
        }, {
          item1: 'F实验室监测',
          item2: 'F4',
          item3: '病原鉴定报告检出荧光假单胞菌',
          item4: ''
        }, {
          item1: 'F实验室监测',
          item2: 'F4',
          item3: '病原鉴定报告检出荧光假单胞菌',
          item4: ''
        }
      ],
      tableThead: [
        { isShow: true, label: '诊断项名称', prop: 'diagnosisItemName' },
        { isShow: true, label: '诊断项症状编号', prop: 'diagnosisItemSymbolNo' },
        { isShow: true, label: '特征描述', prop: 'diagnosisItemSymbolDesc' },
        { isShow: true, label: '特征图片', slotName: 'fileIdList' }
      ],
      // 配置项
      table_config: {
        isCheckBox: true,

        isOperation: true,
        // 表格操作列
        operation: {
          label: '操作',
          width: '200',
          cols: [
            {
              label: '编辑',
              type: 'primary',
              flag: 'edit',
              handleRow: this.handleRow
            },
            {
              label: '删除',
              type: 'danger',
              flag: 'delete',
              handleRow: this.handleRow
            }
          ]
        },
      },
      varietyList: [],
      diagnosisItemList: [],
      formItemList: [
        {
          label: '诊断项名称',
          type: 'select',
          selectOptions: [],
          param: 'diagnosisItemId',
          defaultSelect: '', // 下拉框默认选中项
          multiple: false,
          isShow: true
        },
        {
          label: '症状编号',
          type: 'input',
          param: 'diagnosisItemSymbolNo',
          defaultSelect: '', // 下拉框默认选中项
          isShow: true
        }
      ]
    }
  },
  mounted() {
    this.queryPageByCondition();
    this.initData();
  },
  methods: {
    async initData() {
      //获取品种；
      let { data: varietyList } = await varietyManagement.queryByCondition({})
      this.varietyList = varietyList || [];
      let { data: diagnosisItemList } = await diagnosticItemManagement.queryByCondition({})
      this.diagnosisItemList = diagnosisItemList || []
      console.log(this.diagnosisItemList)
      this.setFormOptions();
    },
    setFormOptions() {
      let list = JSON.parse(JSON.stringify(this.formItemList));
      this.formItemList = []
      list.forEach((item, index) => {
        if (item.param.indexOf('diagnosisItemId') !== -1) {
          let selectOptions = this.diagnosisItemList.map(data => {
            return {
              text: data.diagnosisItemName, value: data.diagnosisItemId
            }
          })
          item.defaultSelect = this.searchFormData[item.param];
          item.selectOptions = selectOptions;
        };
      });
      this.$nextTick(() => {
        this.$set(this, 'formItemList', list)
        console.log(this.formItemList)
      })
    },
    delList(deleteList = [], key = 'diagnosisItemSymbolId') {
      let list = []
      deleteList.forEach(item => {
        list.push(item[key])
      })
      console.log(list)
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await diagnosticItemSymptom.delList(list);
        if (res.code == 1) {
          this.$message.success(res.msg || '删除成功');
          this.queryPageByCondition();
        } else {
          this.$message.warning(res.msg || '删除失败');
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    queryPageByCondition() {
      let params = {
        page: this.page,
        size: this.size,
        entity: {
          ...this.searchFormData
        }
      }
      diagnosticItemSymptom.queryPageByCondition(params).then(res => {
        if (res.code == 1) {
          this.dataList = res.data.content || [];
          this.dataList.forEach(item => {
            item.imgList = [];
            let fileIdList = item.fileIdList.split(',')
            fileIdList.forEach(id => {
              item.imgList.push(`${this.$baseurl.fileServiceUrl}fish/public/files/download?fileId=${id}`)
            })
            console.log(item.imgList)
          })
          this.total = res.data.totalElements;
          if (res.data.totalPages && this.page > res.data.totalPages) {
            this.page = res.data.totalPages;
            this.queryPageByCondition();
          }
        } else {
          this.$message.warning(res.msg || '分页查询失败')
        }
      }).catch(err => {
        console.log(err)
      })
    },
    search(data) {
      this.page = 1;
      this.searchFormData ={...this.searchFormData,...data };
      this.queryPageByCondition();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryPageByCondition();
    },
    handleSizeChange(size) {
      this.size = size;
      this.queryPageByCondition();
    },
    // 操作方法
    handleRow(rowData, item) {
      if (item.flag == 'edit') {
        this.dialogVisible = true;
        this.formData = JSON.parse(JSON.stringify(rowData))
      } else if (item.flag == 'delete') {
        this.delList([rowData]);
      }
      this.addEditFlag = item.flag
    },
    // 多选方法
    selectionChange(val) {
      this.selectList = val || []
    },
    //获取选中Btn
    getBtnEvent(flag) {
      console.log(flag)
      if (flag == 'add') {
        this.dialogVisible = true
      } else if (flag == 'delete') {
        if (this.selectList.length > 0) {
          this.delList(this.selectList)
        } else {
          this.$message.warning('请先选择表单数据项后再删除！')
        }
      }
      this.addEditFlag = flag
    },
    //关闭弹窗
    closeDialog() {
      this.dialogVisible = false;
      this.queryPageByCondition();
    },
  }
};
</script>
<style lang="scss" scoped>
.pointer {
  cursor: pointer;
}

.imageStyle {
  height: 50px;
  width: 50px;
  border-radius: 4px;
}
</style>
  
  